{{- $name := .Chart.Name }}
{{- range $index, $val := .Values.items }}
{{- if eq $val.type "service" }}
{{- if $val.services | required " For '.type: service' ned .service ! " }}
apiVersion: v1
kind: Service
metadata:
  name: {{ $val.name | default (print $name "-service-" $index) }}
  labels:
    app: {{ $name }}
    tier: {{ $val.name | default (print $name "-service") }}
spec:
{{- range $_, $Val := $val.services }}
  type: {{ $Val.type | default (print "ClusterIP") }}
  ports:
  {{- toYaml $Val.ports | nindent 2 }}
  {{- if .selector | required " For services need services.selecor! " }}
  selector:
    {{- toYaml $Val.selector | nindent 4 }}
  {{- end }}
{{- end }}
{{ print "---" }}
{{- end }}{{- end }}{{- end }}
{{- range $index, $val := .Values.items }}
{{- if not (empty $val.containers) }}
{{- range $Index, $Containers := $val.containers }}
{{- if $Containers.url }}
apiVersion: v1
kind: Service
metadata:
  name: {{ $val.name | default (print $name "-service-" $index "-" $Index) }}
  labels:
    app: {{ $name }}
    tier: {{ $val.name | default (print $name "-service") }}
spec:
  type: {{ print "ClusterIP" }}
  ports:
  {{- range $indexPorts, $Ports := $Containers.ports }}
  - name: {{ $Ports.name | default (printf "3%v%v%v-%v" $index $Index $indexPorts $name) }}
    port: {{ printf "3%v%v%v" $index $Index $indexPorts }}
    protocol: {{ $Ports.protocol | default (print "TCP") }}
    targetPort: {{ $Ports.containerPort }}
  {{- end }}
  selector:
    app: {{ $name }}
    tier: {{ $val.name | default (print $name "-" $val.type "-" $index) }}
{{ print "---" }}
{{- end }}{{- end }}{{- end }}{{- end }}